package com.one.efaimaly.main.presenter;

import android.content.Context;

import com.imnjh.imagepicker.util.LogUtils;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.refreshlayout.utils.LogUtil;
import com.one.efaimaly.main.model.MainModel;
import com.one.common.model.bean.BannerBean;
import com.one.efaimaly.main.model.item.BannerItem;
import com.one.efaimaly.main.model.item.OrderItem;
import com.one.efaimaly.order.model.OrderModel;

import java.util.ArrayList;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 下午8:26
 * description ：
 **/
public class MainPresenter extends BaseApiPresenter<IListView, MainModel> {
    private String orderState;

    public MainPresenter(IListView mView, Context mContext) {
        super(mView, mContext, new MainModel((BaseActivity) mContext));
    }
    private ArrayList<Object> loadData = new ArrayList<>();
    /**
     * 获取数据
     */
    public void getData(String state) {
        orderState = state;
        if (mView == null) {
            return;
        }
        if (mView.getPage() == 1) {
            getBanner();
        } else {
            getOrderList(mView.getPage() + "", state,false);
        }
    }

    /**
     * 获取首页banner
     */
    private void getBanner() {
        mModel.getBanner(new ObserverOnResultListener<ArrayList<BannerBean>>() {
            @Override
            public void onResult(ArrayList<BannerBean> bannerBeans) {
                if (mView != null) {
                    loadData.clear();
                    loadData.add(new BannerItem(bannerBeans));
                    getOrderList(mView.getPage() + "", orderState,true);

                }
            }
        });
    }

    /**
     * 获取订单列表
     */
    public void getOrderList(String page, String state,boolean isAddBanner) {
        mModel.getOrderList(page, state, new ObserverOnResultListener<ArrayList<OrderItem>>() {
            @Override
            public void onResult(ArrayList<OrderItem> orderItems) {
                if (mView != null) {
                    if(mView.getPage() == 1) {
                        if(isAddBanner){
                            loadData.addAll(orderItems);
                            mView.loadSuccess(loadData);
                        }else{
                            mView.loadSuccess(orderItems);
                        }
                    } else {
                        mView.loadSuccess(orderItems);
                    }
                }
            }
        });
    }

    /**
     * 取消订单
     */
    public void cancel(String id){
        new OrderModel(mActivity).cancel(id, new ObserverOnResultListener<String>() {
            @Override
            public void onResult(String s) {
               if(mView != null){
                   mView.doRefresh();
               }
            }
        });
    }

}
